#include "Utils.hpp"
std::vector<SDL_Surface*> Utils::surs;
SDL_Texture *Utils::tex_black=nullptr;
SDL_Texture *Utils::tex_white=nullptr;
TTF_Font *Utils::font=nullptr;
bool Utils::initSDL()
{
    if(SDL_Init(SDL_INIT_EVERYTHING)){
        return false;
    }
    if(!IMG_Init(IMG_INIT_PNG)){
        return false;
    }
    if(TTF_Init()){
        return false;
    }
    return true;
}

void Utils::quitSDL()
{
    for(auto a:surs){
        SDL_FreeSurface(a);
    }
    TTF_CloseFont(font);
    surs.clear();
    TTF_Quit();
    IMG_Quit();
    SDL_Quit();
}

void Utils::initResources(SDL_Renderer *render)
{
    SDL_Surface *img = IMG_Load("resources/black.png");
    if (!img)
    {
        SDL_Log("IMG_Load failed");
    }
    surs.push_back(img);
    Utils::tex_black = SDL_CreateTextureFromSurface(render, img);
    img = IMG_Load("resources/white.png");
    if (!img)
    {
        SDL_Log("IMG_Load failed");
    }
    surs.push_back(img);
    Utils::tex_white = SDL_CreateTextureFromSurface(render, img);

    if (!(font = TTF_OpenFont("resources/msyh.ttc", 20)))
    {
        SDL_Log("字体加载失败");
        return;
    }
}
